package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class RoomIconTypeAdapter  extends AppAdapter<Map> {
    private Map<String, Map> mSelected = new LinkedHashMap<>();
    private boolean isSelect = true;
    private boolean isSingleSelect = false;
    private String roomType = "";
    public RoomIconTypeAdapter(Context context) {
        super(context);
    }

    public RoomIconTypeAdapter(@NonNull @NotNull Context context, boolean isSelect, boolean isSingleSelect, String roomType) {
        super(context);
        this.isSelect = isSelect;
        this.isSingleSelect = isSingleSelect;
        this.roomType = roomType != null ? roomType : "";
    }

    public RoomIconTypeAdapter(@NonNull @NotNull Context context, boolean isSelect, boolean isSingleSelect, String roomType,Map<String, Map> data) {
        super(context);
        this.isSelect = isSelect;
        this.isSingleSelect = isSingleSelect;
        this.roomType = roomType != null ? roomType : "";
        this.mSelected = data;
    }

    public Map<String, Map> getSelectMap(){
        return mSelected;
    }

    public void initSelectMap(Map<String, Map> data){
        mSelected = data;

    }


    public List<Map> getSelectedData() {
        List<Map> checkList = new ArrayList<>();
        if (mSelected == null) return checkList;
        for (String key : mSelected.keySet()) {
            checkList.add(mSelected.get(key));
        }
        return checkList;
    }

    @NonNull
    @Override
    public RoomIconTypeAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RoomIconTypeAdapter.ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView iconTypeName;
        private final RecyclerView iconRecyclerView;
        private final RoomIconAdapter iconAdapter;

        private ViewHolder() {
            super(R.layout.room_icon_type_item);
            iconTypeName = findViewById(R.id.tv_floor_name);
            iconRecyclerView = findViewById(R.id.room_recycler_view);
            iconRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 4);
            iconRecyclerView.setLayoutManager(gridLayoutManager);
            iconAdapter = new RoomIconAdapter(getContext()){
                @Override
                public void onSelectChanged(Map<String, Map> selected) {
                    super.onSelectChanged(selected);
                    RoomIconTypeAdapter.this.onSelectChanged(selected);
                }
            };
            iconAdapter.setIsSelect(isSelect);
            iconAdapter.setSingleSelect(isSingleSelect);
            iconRecyclerView.setAdapter(iconAdapter);
            if(!roomType.equals("")){
                Map data = new HashMap<>();
                mSelected.put(roomType,data);
            }
        }

        @Override
        public void onBindView(int position) {
            Map iconData = getItem(position);
            //默认楼层不显示
            iconTypeName.setText(iconData.get("sectionName").toString());
            iconAdapter.setSelected(mSelected);
            iconAdapter.setIsSelect(isSelect);
            iconAdapter.setSelected(mSelected);
            iconAdapter.setData((List<Map>) iconData.get("typeData"));
        }
    }

    public void clearSelected(){
        mSelected.clear();
        notifyDataSetChanged();
    }

    public void changeIsSelect(boolean value){
        isSelect = value;
        notifyDataSetChanged();
    }

    public void changeIsSingleSelect(boolean value){
        isSingleSelect = value;
        notifyDataSetChanged();
    }

    //外部实现
    public void onSelectChanged(Map<String, Map> selectBean){
        notifyDataSetChanged();
    }



}
